package com.example.lancooznbk.view.activity;

import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.example.lancooznbk.view.fragment.MenuDelegatorFragment;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by tangminglong on 17/9/19.
 */

public class MenuManagerActivity extends RxAppCompatActivity {

    private boolean mOptionMenuCreated;
    private Set<MenuDelegatorFragment> mRegisteredFragments = new HashSet<MenuDelegatorFragment>();
    private final Handler handler =  new Handler(Looper.getMainLooper());
    private boolean mInvalidatePosted;

    public boolean registerFragmentForOptionMenu(MenuDelegatorFragment fragment) {
        if (mRegisteredFragments.add(fragment)) {
            if (mOptionMenuCreated) {
                postInvalidateOptionsMenu();
            }
            return true;
        }
        return false;
    }

    public boolean unregisterFragmentForOptionMenu(MenuDelegatorFragment fragment) {
        if (mRegisteredFragments.remove(fragment)) {
            postInvalidateOptionsMenu();
            return true;
        }
        return false;
    }

    /**
     * Method needed cause subsequents calls of {@link #invalidateOptionsMenu()} on Android pre 4.4
     * will get eaten and ignored by the framework.
     */
    private void postInvalidateOptionsMenu() {
        if (mInvalidatePosted) {
            return;
        }
        mInvalidatePosted = true;
        handler.post(new Runnable() {
            @Override
            public void run() {
                mInvalidatePosted = false;
                supportInvalidateOptionsMenu();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        boolean displayMenu = super.onCreateOptionsMenu(menu);
        final MenuInflater menuInflater = new MenuInflater(this);
        for (MenuDelegatorFragment f : mRegisteredFragments) {
            f.onCreateOptionsMenu(menu, menuInflater);
        }
        mOptionMenuCreated = true;
        return displayMenu;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        boolean displayMenu = super.onPrepareOptionsMenu(menu);
        for (MenuDelegatorFragment f : mRegisteredFragments) {
            f.onPrepareOptionsMenu(menu);
        }
        return displayMenu;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // this may be weird behavior: I let the call reach both the activity both any single fragment registered for option menu
        boolean handled = super.onOptionsItemSelected(item);
        for (MenuDelegatorFragment f : mRegisteredFragments) {
            handled |= f.onOptionsItemSelected(item);
        }
        return handled;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mRegisteredFragments.clear();
    }
}
